package com.rocket.excel;

import java.io.File;
import java.util.ArrayList;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.rocket.database.Missile83Sql;
import com.rocket.entity.Missile83;

public class Missile83ExcelUtil extends ExcelUtil {

	private static Missile83ExcelUtil	mInstance;

	public static synchronized Missile83ExcelUtil getInstance() {
		if (mInstance == null) {
			mInstance = new Missile83ExcelUtil();
		}
		return mInstance;
	}

	public void export(final String path) {
		new Thread() {

			public void run() {

				File file = new File(path);

				try {
					WritableWorkbook wwb = Workbook.createWorkbook(file);
					WritableSheet sheet = wwb.createSheet("83型炮弹", 0);

					sheet.mergeCells(0, 0, 0, 2);
					sheet.mergeCells(1, 0, 9, 0);
					sheet.mergeCells(1, 1, 9, 1);

					sheet.addCell(new Label(0, 0, "高炮发射仰角"));
					sheet.addCell(new Label(1, 0, "引信自炸时间（秒）"));
					sheet.addCell(new Label(1, 1, "炸点高度（米）/水平距离（米）"));
					sheet.addCell(new Label(0, 3, "85°"));
					sheet.addCell(new Label(0, 4, "80°"));
					sheet.addCell(new Label(0, 5, "75°"));
					sheet.addCell(new Label(0, 6, "70°"));
					sheet.addCell(new Label(0, 7, "65°"));
					sheet.addCell(new Label(0, 8, "60°"));
					sheet.addCell(new Label(0, 9, "55°"));
					sheet.addCell(new Label(0, 10, "50°"));
					sheet.addCell(new Label(0, 11, "45°"));
					sheet.addCell(new Label(1, 2, "8"));
					sheet.addCell(new Label(2, 2, "10"));
					sheet.addCell(new Label(3, 2, "12"));
					sheet.addCell(new Label(4, 2, "14"));
					sheet.addCell(new Label(5, 2, "16"));
					sheet.addCell(new Label(6, 2, "18"));
					sheet.addCell(new Label(7, 2, "20"));

					int[] angles = new int[] { 85, 80, 75, 70, 65, 60, 55, 50, 45 };
					for (int j = 0; j < angles.length; j++) {
						ArrayList<Missile83> missiles = Missile83Sql.getInstance().queryWithAngle(angles[j]);
						for (int i = 0; i < missiles.size(); i++) {
							Missile83 missile = missiles.get(i);
							sheet.addCell(new Label(i + 1, j + 3, missile.height + "/" + missile.length));
						}

					}

					// //作用是从(m,n)到(p,q)的单元格全部合并，比如：
					// WritableSheet sheet=wwb.createSheet("第一页",0);
					//
					// //合并第一列第一行到第六列第一行的所有单元格
					// sheet.mergeCells(0,0,5,0);

					wwb.write();
					wwb.close();
				} catch (Exception e) {
					e.printStackTrace();
				}

			};
		}.start();
	}
}
